const axios = require('axios')
const Router = require('koa-router')
const { User } = require('../model/index')
const { appid, appsecret } = require('../meta')
const userRouter = new Router({ prefix: '/user' })

userRouter.get('/', async ctx => {
	ctx.body = 'Hello World'
})

userRouter.get('/find', async ctx => {
	const { openid } = ctx.request.query

	await User.findOne({ openid }).then(user => {
		if (user) {
			ctx.body = {
				code: 200,
				data: user,
				message: '查找用户成功',
			}
		} else {
			ctx.body = {
				code: 204,
				data: null,
				message: '未查询到用户',
			}
		}
	})
})

userRouter.post('/create', async ctx => {
	const {
		name = '',
		username = '',
		password = '',
		avatar_url = '',
		openid,
		gender = 0,
		nick_name = '',
		uid = '',
		organization = '',
		university = '',
		sign = '',
		phone_number = '',
	} = ctx.request.body

	await User.create({
		name,
		username,
		password,
		avatar_url,
		openid,
		gender,
		nick_name,
		uid,
		organization,
		university,
		sign,
		phone_number,
	})
		.then(res => {
			if (!res) {
				ctx.body = {
					code: 301,
					message: '添加失败',
				}
			} else {
				ctx.body = {
					code: 200,
					message: '添加成功',
				}
			}
		})
		.catch(() => {
			ctx.body = {
				code: 401,
				message: '注册出现问题',
			}
		})
})

userRouter.get('/openid', async ctx => {
	const { code } = ctx.request.query

	const result = await axios({
		url: 'https://api.weixin.qq.com/sns/jscode2session',
		method: 'GET',
		params: {
			appid,
			secret: appsecret,
			js_code: code,
			grant_type: 'authorization_code',
		},
	})

	ctx.body = result.data
})

/**
 * 小程序端登录
 */
userRouter.get('/login', async ctx => {
	const { openid } = ctx.request.query

	await User.findOne({ openid }).then(loginResult => {
		if (!loginResult) {
			ctx.body = {
				code: 302,
				data: null,
			}
		} else {
			ctx.body = {
				code: 200,
				data: loginResult,
			}
		}
	})
})

/**
 * 后台登录
 */
userRouter.post('/login', async ctx => {
	const { username, password } = ctx.request.body

	const user = await User.findOne({ username, password })

	if (user) {
		ctx.body = {
			code: 200,
			message: '登录成功',
			data: user,
		}
	} else {
		ctx.body = {
			code: 300,
			message: '用户名或密码错误',
			data: null,
		}
	}
})

/**
 * 后台注册
 */
userRouter.post('/register', async ctx => {
	const {
		name = '',
		username = '',
		password = '',
		avatar_url = '',
		openid = '',
		gender = 0,
		nick_name = '',
		uid = '',
		organization = '',
		university = '',
		sign = '',
		phone_number = '',
	} = ctx.request.body

	const user = await User.findOne({ username })

	if (user) {
		ctx.body = {
			code: 204,
			message: '这个用户名已经有人用了, 换一个吧',
		}
		return
	}

	console.log('查找用户名', user)

	await User.create({
		name,
		username,
		password,
		nick_name,
		university,
		organization,
		phone_number,
		openid,
		uid,
		sign,
		gender,
		avatar_url
	})
		.then(result => {
			ctx.body = {
				code: 200,
				message: '注册成功',
				data: result,
			}
		})
		.catch(err => {
			ctx.body = {
				code: 400,
				message: '注册新用户出错' + err.message,
				data: null,
			}
		})
})

module.exports = userRouter
